﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RightRegistrationAddin.Window
{
    public partial class frmContracts : Form
    {
        private System.Drawing.Color m_COLOR_ErrorInserted = System.Drawing.Color.Yellow;
        private System.Drawing.Color m_COLOR_ErrorNotInserted = System.Drawing.Color.Salmon;
        private System.Drawing.Color m_COLOR_Inserted = System.Drawing.Color.LightGray;
        private System.Drawing.Color m_COLOR_NotInserted = System.Drawing.Color.Lime;
        private System.Drawing.Color m_COLOR_BaseInserted = System.Drawing.Color.Cyan;

        private const String m_DESCRICAO_TIPODIREITO1 = "http://www.bovespa.com.br/sinacor/authorization/system";
        private const String m_DESCRICAO_TIPODIREITO2 = "http://www.bovespa.com.br/sinacor/authorization/business";
        private const String m_DESCRICAO_TIPODIREITO3 = "http://www.bovespa.com.br/sinacor/authorization/function";


        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="frmContracts"/> class.
        /// </summary>
        public frmContracts()
        {
            InitializeComponent();
            this.dtgContract.ClearSelection();
        }

        #endregion

        #region Méthods Public

        /// <summary>
        /// Initializes the grid.
        /// </summary>
        /// <param name="contracts">The contracts.</param>
        public static void InitializeGrid(List<Classes.Contract> contracts)
        {
            frmContracts frm = new frmContracts();
            if (contracts != null && contracts.Count > 0)
            {
                frm.dtgContract.AutoGenerateColumns = false;
                frm.dtgContract.DataSource = contracts;
                frm.dtgContract.Columns.Clear();
                frm.dtgContract.Columns.Add("ServiceContract", "Descrição");
                frm.dtgContract.Columns[0].DataPropertyName = "ServiceContract";
                frm.dtgContract.Columns.Add("DescricaoTipoDireito", "Tipo Direito");
                frm.dtgContract.Columns[1].DataPropertyName = "DescricaoTipoDireito";
                frm.dtgContract.Columns.Add("TipoOperacao", "Tipo Operação");
                frm.dtgContract.Columns[2].DataPropertyName = "TipoOperacao";
                frm.dtgContract.Columns.Add("Status", "Status");
                frm.dtgContract.Columns[3].DataPropertyName = "Status";
                frm.dtgContract.Columns[3].Visible = false;
                frm.dtgContract.Columns.Add("TipoDireito", "Tipo Direito");
                frm.dtgContract.Columns[4].DataPropertyName = "TipoDireito";
                frm.dtgContract.Columns[4].Visible = false;

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    Classes.Commom objCommom = new Classes.Commom();
                    if (objCommom.SaveServiceContracts(contracts))
                        MessageBox.Show("Inclusão realizada com sucesso.");
                    else
                        MessageBox.Show("Nenhum registro incluído.");
                }
            }
        }

        #endregion

        #region Methods Private

        /// <summary>
        /// Converts the color of the back.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <returns></returns>
        private System.Drawing.Color ConvertBackColor(Enums.Status status)
        {
            System.Drawing.Color colorBack;

            switch (status)
            {
                case Enums.Status.ErrorInserted:
                    colorBack = m_COLOR_ErrorInserted;
                    break;
                case Enums.Status.ErrorNotInserted:
                    colorBack = m_COLOR_ErrorNotInserted;
                    break;
                case Enums.Status.Inserted:
                    colorBack = m_COLOR_Inserted;
                    break;
                case Enums.Status.NotInserted:
                    colorBack = m_COLOR_NotInserted;
                    break;
                case Enums.Status.BaseInserted:
                    colorBack = m_COLOR_BaseInserted;
                    break;
                default:
                    colorBack = System.Drawing.Color.White;
                    break;
            }

            return colorBack;
        }

        /// <summary>
        /// Converts the tipo direito.
        /// </summary>
        /// <param name="tipoDireito">The tipo direito.</param>
        /// <returns></returns>
        private String ConvertTipoDireito(Int32 tipoDireito)
        {
            String descricaoDireito = "";

            switch (tipoDireito)
            {
                case 1:
                    descricaoDireito = m_DESCRICAO_TIPODIREITO1;
                    break;
                case 2:
                    descricaoDireito = m_DESCRICAO_TIPODIREITO2;
                    break;
                case 3:
                    descricaoDireito = m_DESCRICAO_TIPODIREITO3;
                    break;
                default:
                    descricaoDireito = string.Empty;
                    break;
            }

            return descricaoDireito;
        }

        #endregion

        #region Events Buttons

        /// <summary>
        /// Handles the Click event of the btnConfirmar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnConfirmar_Click(object sender, EventArgs e)
        {
                DialogResult respostaCadastro = MessageBox.Show("Somente os registros com status \"Não Cadastrado\" serão cadastrados, deseja continuar?, ", "Confirmar Cadastro", MessageBoxButtons.YesNo);

                if (respostaCadastro == DialogResult.Yes)
                    this.DialogResult = DialogResult.OK;
                else
                    this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Handles the Click event of the btnCancelar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        #endregion

        #region Events DataGrid

        /// <summary>
        /// Handles the DataBindingComplete event of the dtgContract control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewBindingCompleteEventArgs"/> instance containing the event data.</param>
        private void dtgContract_DataBindingComplete(object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
        {
            if(this.dtgContract.RowCount > 0)
            {
                Classes.Commom objCommom = new Classes.Commom();
                System.Drawing.Color colorBack;
            
                for (int intI = 0; intI < this.dtgContract.RowCount; intI++)
                {
                    colorBack = ConvertBackColor((Enums.Status)this.dtgContract[3, intI].Value);

                    this.dtgContract[0, intI].Style.BackColor = colorBack;
                    this.dtgContract[1, intI].Style.BackColor = colorBack;
                    this.dtgContract[2, intI].Style.BackColor = colorBack;
                    this.dtgContract[1, intI].Value = ConvertTipoDireito((Int32)this.dtgContract[4, intI].Value);
                    this.dtgContract[2, intI].Value = objCommom.ConvertTypeOperation((String)this.dtgContract[2, intI].Value);
                }
            }

            this.dtgContract.ClearSelection();
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        /// <summary>
        /// Handles the CellClick event of the dtgContract control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
        private void dtgContract_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            this.dtgContract.ClearSelection();
        }


        #endregion

    }
}
